package com.bsoft.gol.hcs.specialist.service.impl;

import ctd.controller.exception.ControllerException;
import ctd.util.annotation.RpcService;
import hcn.util.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.bsoft.gol.hcs.specialist.bean.response.HealthEducationList;
import com.bsoft.gol.hcs.specialist.dao.FusHealthEducationDAO;
import com.bsoft.gol.hcs.specialist.dao.FusHealthEducationRelationDAO;
import com.bsoft.gol.hcs.specialist.entity.FusHealthEducationEntity;
import com.bsoft.gol.hcs.specialist.entity.FusHealthEducationRelation;
import com.bsoft.gol.hcs.specialist.service.IFusHealthEducationService;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.List;


/**
 * 随访系统_健康宣教库_数据访问层接口——实现
 *
 * @author tongtp
 * @version v0.1
 * @create 2018-11-11 14:32
 **/
@Service
public class FusHealthEducationServiceImpl implements IFusHealthEducationService {

    protected static final Logger log = LoggerFactory.getLogger(FusHealthEducationServiceImpl.class);
    @Autowired
    FusHealthEducationDAO fusHealthEducationDAO;
    @Autowired
    FusHealthEducationRelationDAO fusHealthEducationRelationDAO;

    /**
     * 根据id和type查询到健康宣教材料对应列表
     *
     * @param id   健康宣教id
     * @param type 健康宣教类型
     * @return 健康宣教材料对应列表
     */
    @Override
    @RpcService
    public List<FusHealthEducationEntity> findFusHealthEducationListByIdAndType(Long id, Integer type) throws ControllerException {
        if(CommonUtils.isEmpty(id)||CommonUtils.isEmpty(type)){
            throw new ControllerException(ResultCode.ERROR,"入参不能为空");
        }
        return fusHealthEducationDAO.findFusHealthEducationListByIdAndType(id, type);
    }

    /**
     * 根据id查询到健康宣教材料详情
     *
     * @param id 健康宣教id
     * @return 健康宣教材料对应类型的子列表
     */
    @Override
    @RpcService
    public List<FusHealthEducationEntity> findFusHealthEducationSubListByIdAndType(Long id) throws ControllerException {
        if (CommonUtils.isEmpty(id)) {
            throw new ControllerException(ResultCode.ERROR, "入参不能为空");
        }
        return fusHealthEducationDAO.findFusHealthEducationSubListByIdAndType(id);
    }

    /**
     * 通过id查询健康宣教列表
     *
     * @param educationIdList
     * @return 宣教列表
     */
    @Override
    public List<FusHealthEducationEntity> findFusHealthEducationSubListByIdList(List<Long> educationIdList) throws ControllerException {
        if (CommonUtils.isEmpty(educationIdList)) {
            throw new ControllerException(ResultCode.ERROR, "入参不能为空");
        }
        return fusHealthEducationDAO.findFusHealthEducationSubListByIdList(educationIdList);
    }

    /**
     * 根据科室id查询到健康宣教列表
     *
     * @param departmentId 科室id
     * @return
     */
    @Override
    @RpcService
    public List<FusHealthEducationEntity> findFusHealthEducationSubListByDepartmentId(String departmentId) {
        if (StringUtils.isEmpty(departmentId)) {
            return fusHealthEducationDAO.findFusHealthEducationSubAllList();
        }
        return fusHealthEducationDAO.findFusHealthEducationSubListByDepartmentId(departmentId);
    }

    /**
     * 根据机构id查询健康宣教列表
     * @param orgId
     * @return
     */
    @Override
    public List<FusHealthEducationEntity> findFusHealthEducationSubListByOrgId(String orgId) {
        if (StringUtils.isEmpty(orgId)) {
            return fusHealthEducationDAO.findFusHealthEducationSubAllList();
        }
        return fusHealthEducationDAO.findFusHealthEducationSubListByOrgId(orgId);
    }

    /**
     * 根据机构id和departmentId查询健康宣教列表
     * @param orgId
     * @return
     */
    @Override
    public List<FusHealthEducationEntity> findListByOrgIdAndDepartmentId(String orgId, String departmentId) {
        if (CommonUtils.isEmpty(orgId) && CommonUtils.isEmpty(departmentId)) {
            return fusHealthEducationDAO.findFusHealthEducationSubAllList();
        }
        return fusHealthEducationDAO.findListByOrgIdAndDepartmentId(orgId, departmentId);
    }


    /**
     * @Description:查询到健康宣教库的顶级目录层级
     * @param: []
     * @return: java.util.List<com.bsoft.platform.model.FusHealthEducation>
     * @author:tongtp
     * @Date: 2018/11/11
     */
    @Override
    @RpcService
    public List<FusHealthEducationEntity> findFusHealthEducationTopCatalog() {
        return fusHealthEducationDAO.findFusHealthEducationTopCatalog();
    }

    /**
     * 查询到健康宣教库目录下的子目录或内容
     *
     * @param fusHealthEducationId 健康宣教id
     * @return 健康宣教信息
     */
    @Override
    @RpcService
    public List<FusHealthEducationEntity> findLowerFusHealthEducationList(Long fusHealthEducationId) throws ControllerException {
        if(CommonUtils.isEmpty(fusHealthEducationId)){
            throw new ControllerException(ResultCode.ERROR,"入参不能为空");
        }
        return fusHealthEducationDAO.findLowerFusHealthEducationList(fusHealthEducationId);
    }


    /**
     * 新增健康宣教库数据
     *
     * @param fusHealthEducation 健康宣教入参
     */
    @Override
    @RpcService
    public FusHealthEducationEntity insertFusHealthEducation(FusHealthEducationEntity fusHealthEducation) {
        return fusHealthEducationDAO.save(fusHealthEducation);
    }

    @Override
    @RpcService
    public void insertFusHealthEducationRelation(FusHealthEducationRelation fusHealthEducationRelation) {
        fusHealthEducationRelationDAO.save(fusHealthEducationRelation);
    }

    /**
     * 根据ID更新健康宣教库数据
     *
     * @param fusHealthEducation 健康宣教入参
     */
    @Override
    @RpcService
    public void updateFusHealthEducationById(FusHealthEducationEntity fusHealthEducation) {
        fusHealthEducationDAO.updateFusHealthEducationById(fusHealthEducation);
    }

    /**
     * 根据ID删除健康宣教库数据
     *
     * @param fusHealthEducationId 健康宣教id
     */
    @Override
    @RpcService
    public void deleteFusHealthEducationById(Long fusHealthEducationId) throws ControllerException {
        if(CommonUtils.isEmpty(fusHealthEducationId)){
            throw new ControllerException(ResultCode.ERROR,"入参不能为空");
        }
        fusHealthEducationDAO.deleteFusHealthEducationById(fusHealthEducationId);
    }

    @Override
    @RpcService
    public void deleteHealthEducationRelationByFusHealthEducationId(Long fusHealthEducationId) throws ControllerException {
        if(CommonUtils.isEmpty(fusHealthEducationId)){
            throw new ControllerException(ResultCode.ERROR,"入参不能为空");
        }
        fusHealthEducationRelationDAO.deleteHealthEducationRelationByFusHealthEducationId(fusHealthEducationId);
    }

    /**
     * 获取健康宣教材料详情
     *
     * @param fusHealthEducationId 健康宣教Id
     * @return 健康宣教内容
     */
    @Override
    @RpcService
    public FusHealthEducationEntity getFusHealthEducationContent(Long fusHealthEducationId) throws ControllerException {
        if(CommonUtils.isEmpty(fusHealthEducationId)){
            throw new ControllerException(ResultCode.ERROR,"入参不能为空");
        }
        return fusHealthEducationDAO.getFusHealthEducationContent(fusHealthEducationId);
    }

    /**
     * 目录上移
     *
     * @param fusHealthEducationId 健康宣教Id
     * @param sort                 当前排序
     * @param parentId             父级id
     */
    @Override
    @RpcService
    public void upCatalog(Long fusHealthEducationId, Integer sort, Long parentId) throws ControllerException {
        if(CommonUtils.isEmpty(fusHealthEducationId)||CommonUtils.isEmpty(sort)||CommonUtils.isEmpty(parentId)){
            throw new ControllerException(ResultCode.ERROR,"入参不能为空");
        }

        Integer upSort = sort - 1;
        //1.上一格目录下移一格
        fusHealthEducationDAO.updateLastCatalogSort(upSort, parentId);
        //2.当前目录上移一格
        fusHealthEducationDAO.updateCurrentCatalogSortById(fusHealthEducationId, upSort);
    }

    /**
     * 目录下移
     *
     * @param fusHealthEducationId 健康宣教Id
     * @param sort                 当前排序
     * @param parentId             父级id
     */
    @Override
    @RpcService
    public void downCatalog(Long fusHealthEducationId, Integer sort, Long parentId) throws ControllerException {
        if(CommonUtils.isEmpty(fusHealthEducationId)||CommonUtils.isEmpty(sort)||CommonUtils.isEmpty(parentId)){
            throw new ControllerException(ResultCode.ERROR,"入参不能为空");
        }
        Integer downSort = sort + 1;
        //1.下一格目录上移一格
        fusHealthEducationDAO.updateNextCatalogSort(downSort, parentId);
        //2.当前目录下移一格
        fusHealthEducationDAO.updateCurrentCatalogSortById(fusHealthEducationId, downSort);
    }

    /**
     * 把当前排序后面的目录排序都减一
     *
     * @param parentId 父级id
     * @param sort     当前排序
     */
    @Override
    @RpcService
    public void updateCatalogSort(Long parentId, Integer sort) throws ControllerException {
        if(CommonUtils.isEmpty(sort)||CommonUtils.isEmpty(parentId)){
            throw new ControllerException(ResultCode.ERROR,"入参不能为空");
        }
        fusHealthEducationDAO.updateCatalogSort(sort, parentId);
    }

    @Override
    @RpcService
    public FusHealthEducationEntity getFusHealthEducationByName(String name) throws ControllerException {
        if(CommonUtils.isEmpty(name)){
            throw new ControllerException(ResultCode.ERROR,"入参不能为空");
        }
        return fusHealthEducationDAO.getFusHealthEducationByName(name);
    }

    /**
     * 通过健康宣教id查询病种列表
     *
     * @param fusHealthEducationId 健康宣教id
     * @return 病种列表
     */
    @Override
    @RpcService
    public List<FusHealthEducationRelation> findFusHealthEducationRelations(Long fusHealthEducationId) throws ControllerException {
        if(CommonUtils.isEmpty(fusHealthEducationId)){
            throw new ControllerException(ResultCode.ERROR,"入参不能为空");
        }
        return fusHealthEducationRelationDAO.findFusHealthEducationRelations(fusHealthEducationId);
    }

    /**
     * 通过病种id查询健康宣教列表
     *
     * @param diseaseTypeId 病种id
     * @return 宣教列表
     */
    @Override
    @RpcService
    public List<HealthEducationList> findDiseaseTypeListByDiseeId(Long diseaseTypeId) throws ControllerException {
        if(CommonUtils.isEmpty(diseaseTypeId)){
            throw new ControllerException(ResultCode.ERROR,"入参不能为空");
        }
        return fusHealthEducationRelationDAO.findDiseaseTypeListByDiseeId(diseaseTypeId);
    }

    /**
     * 通过病种id查询健康宣教列表
     *
     * @param diseaseTypeIdList 病种id集合
     * @return 宣教列表
     */
    @Override
    @RpcService
    public List<HealthEducationList> findDiseaseTypeListByDiseeIdarr(List<Long> diseaseTypeIdList) throws ControllerException {
        if(CommonUtils.isEmpty(diseaseTypeIdList)){
            throw new ControllerException(ResultCode.ERROR,"入参不能为空");
        }
        return fusHealthEducationRelationDAO.findDiseaseTypeListByDiseeIdarr(diseaseTypeIdList);
    }

    @Override
    public FusHealthEducationEntity getFusHealthEducationById(Long fusHealthEducationId) throws ControllerException {
        if(CommonUtils.isEmpty(fusHealthEducationId)){
            throw new ControllerException(ResultCode.ERROR,"入参不能为空");
        }
        return fusHealthEducationDAO.get(fusHealthEducationId);
    }
}
